package com.jiyongjun.component;

import com.jiyongjun.component.config.LogBizConfig;
import com.jiyongjun.component.config.LogBootstrapConfig;
import com.jiyongjun.component.enums.LogConstant;
import com.jiyongjun.component.utils.ParamValidator;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.Collections;

import static com.jiyongjun.component.utils.IfUtils.ifNull;

/**
 * <h2></h2>
 * <h3>描述：</h3>
 *
 * @author yongjun.jyj
 * @since 2022/7/14 23:07
 */
@Configuration
@EnableConfigurationProperties({SlsLogBootstrapProperties.class, SlsLogBizProperties.class})
@Import(SlsLogConfiguration.class)
public class SlsLogAutoConfigure {

    @Bean
    public LogBootstrapConfig logBootstrapConfig(SlsLogBootstrapProperties slsLogBootstrapProperties) {
        LogConstant.DEFAULT_LOGGER.info("slsLog启动配置项为:{}", slsLogBootstrapProperties);
        if (slsLogBootstrapProperties.getEnable()) {
            ParamValidator.validate(slsLogBootstrapProperties);
        }

        LogBootstrapConfig config = new LogBootstrapConfig();
        config.setEnable(slsLogBootstrapProperties.getEnable());
        config.setEnv(slsLogBootstrapProperties.getEnv());
        config.setSlsEndpoint(slsLogBootstrapProperties.getSlsEndpoint());
        config.setSlsProject(slsLogBootstrapProperties.getSlsProject());
        config.setSlsLogStore(slsLogBootstrapProperties.getSlsLogStore());
        config.setSlsAccessId(slsLogBootstrapProperties.getSlsAccessId());
        config.setSlsAccessKey(slsLogBootstrapProperties.getSlsAccessKey());

        return config;
    }

    @Bean
    public LogBizConfig slsLogBizConfig(SlsLogBizProperties slsLogBizProperties) {
        LogConstant.DEFAULT_LOGGER.info("slsLog业务配置项为:{}", slsLogBizProperties);

        LogBizConfig config = new LogBizConfig();
        config.setDisabledLogApiType(ifNull(slsLogBizProperties.getDisabledLogApiType(), Collections.emptySet()));
        config.setValidateArgsWithoutValidAnnotation(slsLogBizProperties.getValidateArgsWithoutValidAnnotation());

        return config;
    }
}
